import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class Game implements ActionListener {
	public CardTable cardtable;
	public GUI gui;
	//private HashMap<String, Hand> hands;
	//private Hand hand;
	private static Game theOne;
	private Log log;
	public NCTClient client;
	public PlayerTableModel playerTable;
	public NetworktableServer server;

//	public static void main(String[] args) {
//		getGame();
//	}

	public Game(String hostname, int port, String name, boolean host) {
		theOne = this;
		gui = new GUI();
		if (host) {
			server = new NetworktableServer(port);
			server.start();
		}
		// setting up the menus
		JMenuBar menubar = new JMenuBar();
		JMenu gameMenu = new JMenu("Game");
		JMenuItem menuItem = new JMenuItem("New game");
		gameMenu.add(menuItem);
		menuItem.addActionListener(new MenuListener());
		menuItem = new JMenuItem("Disconnect from game");
		gameMenu.add(menuItem);
		menuItem.addActionListener(new MenuListener());
		menuItem = new JMenuItem("Quit game");
		gameMenu.add(menuItem);
		menuItem.addActionListener(new MenuListener());
		JMenu helpMenu = new JMenu("Help");
		helpMenu.add(new JMenuItem("Show help"));
		helpMenu.add(new JMenuItem("About NetworkCardTable"));
		menubar.add(gameMenu);
		menubar.add(helpMenu);
		gui.setJMenuBar(menubar);

		// setting up the playertable
		String tableHead[] = {"Player", "Hand-Cards"};
		playerTable = new PlayerTableModel();
		// add everything
		log = new Log();
		JSplitPane playerLog = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JTable(playerTable), log);
		playerLog.setResizeWeight(0.5);
		cardtable = new CardTable();
		JSplitPane tableLog = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(cardtable), playerLog);
		tableLog.setResizeWeight(1.);
		//cardtable.addCounter("TESAT", 200, 200);
		cardtable.repaint();
		client = new NCTClient(hostname,name,port);
		client.start();
		System.out.println(client.getOwnHand());
		JSplitPane center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableLog, new JScrollPane(client.getOwnHand()));
		center.setResizeWeight(1.);
		gui.add(center);
		addPlayer(name);
		gui.pack();
	}

	public static Game getGame() {
		if (theOne == null)
			theOne = new Game("localhost", 6666, "Local", true);
		return theOne;
	}

	public Log getLog(){
		return log;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

	}

	public void addPlayer(String name) {
		playerTable.addPlayer(name, 0);
		System.out.println("In Add Player");
		if(client.hands.get(name)==null){
			client.hands.put(name, new Hand());
			gui.repaint();
			gui.validate();
			gui.pack();
		}
	}

	class MenuListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (((JMenuItem) e.getSource()).getLabel().equals("New game")) {
				theOne.gui.setVisible(false); // TODO: Disconnect connections
				if (server != null)
					server.stopServer();
				client.closeConnection();
				new ModeChooser();
			} else if (((JMenuItem) e.getSource()).getLabel().equals("Disconnect from game")) {
				if (server != null)
					server.stopServer();
				client.closeConnection();
			} else if (((JMenuItem) e.getSource()).getLabel().equals("Quit game")) {
				theOne.gui.setVisible(false); // TODO: Disconnect connections
				if (server != null)
					server.stopServer();
				client.closeConnection();
				System.exit(0);
			}
		}
	}

	class PlayerTableModel implements TableModel {
		private ArrayList<Object[]> players;
		private PlayerTableModel() {
			players = new ArrayList<Object[]>();
		}
		@Override
		public void addTableModelListener(TableModelListener l) {
		}
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return columnIndex==0?String.class:Integer.class;
		}
		@Override
		public int getColumnCount() {
			return 2;
		}
		@Override
		public String getColumnName(int columnIndex) {
			return columnIndex==1?"Player":"#Cards";
		}
		@Override
		public int getRowCount() {
			return players.size();
		}
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return players.get(rowIndex)[columnIndex];
		}
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}
		@Override
		public void removeTableModelListener(TableModelListener l) {
		}
		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			players.get(rowIndex)[columnIndex] = aValue;
		}
		public void addPlayer(String name, Integer cards) {
			Object[] player = {name, cards};
			players.add(player);
		}
		public void incrHand(String name, Integer mod) {
			for (int i = 0; i < players.size(); i++) {
				if (players.get(i)[0].equals(name)) {
					System.out.println(players.get(i)[1]);
					players.get(i)[1] = (Integer) players.get(i)[1] + mod;
					System.out.println(players.get(i)[1]);
				}
			}
			gui.repaint();
			gui.validate();
			gui.pack();
		}
	}

}
